package com.xiaochong.bitong.api.controller;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.service.QuotationService;
import com.xiaochong.bitong.api.service.ReportService;
import com.xiaochong.bitong.api.utils.ApiVersion;
import com.xiaochong.bitong.api.utils.ListSortUtil;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.BannerMapper;
import com.xiaochong.tir.common.data.mapper.ExchangeInfoMapper;
import com.xiaochong.tir.common.data.po.Banner;
import com.xiaochong.tir.common.data.po.ExchangeInfo;
import com.xiaochong.tir.common.data.po.SyResearchReport;
import com.xiaochong.tir.common.data.po.SyRotationMap;
import com.xiaochong.tir.common.data.vo.ApiKActionQuantizationVo;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationVo;
import com.xiaochong.tir.common.data.vo.kActionQuantizationParamsVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 币种
 * @author MengXiangDong
 */
@RestController
@Api(value = "行情")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/exchange")
public class QuotationController {

	@Autowired
	private RedisServer redisServer;

	@Autowired
	private ExchangeInfoMapper exchangeInfoMapper;

	@Autowired
	private QuotationService quotationService;

	//k线数据-K线图
	@ApiOperation(value = "交易所列表" , notes = "交易所列表（参数：交易所id，交易对名称，维度）")
	@PostMapping("transactions")
	public ApiResultVo transactions() throws Exception {
		ApiResultVo resultVo = new ApiResultVo();
		try {
			ExchangeInfo exchangeInfo=new ExchangeInfo();
			exchangeInfo.setEnableFlag( true );
			exchangeInfo.setDeleteFlag( false );
			exchangeInfo.setKlineStatus( true );

			List<ExchangeInfo> infoList=exchangeInfoMapper.selectByEntityList( exchangeInfo );
			List<ExchangeInfo> infoList2=new ArrayList<>(  );
			List<ExchangeInfo> infoList3=new ArrayList<>(  );
			List<ExchangeInfo> infoList4=new ArrayList<>(  );

			for (int i=0;i<infoList.size();i++){
				ExchangeInfo info = infoList.get( i );
				if(infoList.get( i ).getSort()!=null){
					infoList2.add( info );
				}else{
					infoList3.add( info );
				}
			}
			ListSortUtil sortUtil=new ListSortUtil();
			sortUtil.mySort( infoList2,"sort","asc" );


			for(int i=0;i<infoList3.size();i++){
				ExchangeInfo info=new ExchangeInfo();
				info.setExchangeName( infoList3.get( i ).getExchangeName() );
				info.setId( infoList3.get( i ).getId() );
				infoList4.add( info );
				infoList3.get( i ).setExchangeName( infoList3.get( i ).getExchangeName().toUpperCase() );
			}
			sortUtil.mySort( infoList3,"exchangeName","asc" );
			for(int i=0;i<infoList3.size();i++){
				for (int j=0;j<infoList4.size();j++){
					if(infoList4.get( j ).getId() .equals(   infoList3.get( i ).getId()) ){
						infoList3.get( i ).setExchangeName( infoList4.get( j ).getExchangeName() );
					}
				}
			}
			infoList2.addAll( infoList3 );
			resultVo.setCode( ReturnCode.SUCCESS);
			resultVo.setTradeId(LogTrace.getTrace().getTraceId());
			resultVo.setData( infoList2 );
			resultVo.setTotal( infoList2.size() );
		}catch (Exception e){
			e.printStackTrace();
			LogTrace.error("交易所列表", e);
		}
		return resultVo;
	}

	@ApiOperation(value = "行情k线", notes = "行情k线")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "条数", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "field", value = "排序字段", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "type", value = "排序方式（desc:降序，asc:升序）", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "exchangeId", value = "交易所Id", required = true, dataType = "Integer", paramType = "query")
	})
	@PostMapping("/kActionQuantization")
	public ApiResultVo<List<ApiKActionQuantizationVo>> kActionQuantization(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
																		   @RequestParam(value = "pageSize", required = true) Integer pageSize,
																		   @RequestParam(value = "field", required = true) String field,
																		   @RequestParam(value = "type", required = true) String type,
																		   @RequestParam(value = "exchangeId", required = true) Integer exchangeId){
		return quotationService.kActionQuantization(pageIndex,pageSize,field,type,exchangeId);
	}

}
